package main

import (
	"github.com/gin-contrib/sessions"
	"github.com/gin-contrib/sessions/cookie"
	"github.com/gin-gonic/gin"
)

//保存session
func SaveAuthSession(c *gin.Context, info interface{}) {
	session := sessions.Default(c)
	session.Set("uid", info)
	// c.SetCookie("user_id",string(info.(map[string]interface{})["b"].(uint)), 1000, "/", "localhost", false, true)
	session.Save()
}

//清除session
func ClearAuthSession(c *gin.Context) {
	session := sessions.Default(c)
	session.Clear()
	session.Save()
}

func main() {
	r := gin.Default()
	//使用session
	store := cookie.NewStore([]byte("secret"))
	r.Use(sessions.Sessions("mysession", store))

	r.GET("/login", func(c *gin.Context) {
		SaveAuthSession(c, "uuid")
		c.JSON(200, gin.H{"code": 0})
	})

	r.GET("/getLoginInfo", func(c *gin.Context) {
		session := sessions.Default(c)

		uid := session.Get("uid")
		c.JSON(200, gin.H{"code": 0, "data": uid})
	})

	r.Run(":3010")
}
